package com.can.controller;

import com.can.convert.TagConvert;
import com.can.domain.ResponseResult;
import com.can.domain.vo.PageVO;
import com.can.domain.vo.TagListVO;
import com.can.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/content/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    @GetMapping("/list")
    public ResponseResult<PageVO> list(Integer pageNum, Integer pageSize, TagListVO tagListVO) {
        return tagService.pageTagList(pageNum, pageSize, tagListVO);
    }

    @GetMapping("/listAllTag")
    public ResponseResult listAllTag() {
        return ResponseResult.okResult(tagService.list());
    }

    @PostMapping
    public ResponseResult save(@RequestBody TagListVO tagListVO) {
        tagService.save(TagConvert.INSTANCE.convert(tagListVO));
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult del(@PathVariable Long id) {
        tagService.removeById(id);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult<TagListVO> get(@PathVariable Long id) {
        return ResponseResult.okResult(TagConvert.INSTANCE.convert01(tagService.getById(id)));
    }

    @PutMapping
    public ResponseResult update(@RequestBody @Valid TagListVO vo) {
        tagService.updateById(TagConvert.INSTANCE.convert(vo));
        return ResponseResult.okResult();
    }

}